/**
 * Created by ih220 on 2016/7/21.
 */

/*
 控制器
 */
/*
 添加类别控制器
 1.0 mouse 2016/07/19
 */
appController.controller("addSortSetCtrl",['$scope','isNaN','nameLength',function($scope,isNaN,nameLength){
 /*获取分类*/
 //alert();
 var childer=[];
 var childer2=[];

$scope.getSort=function(){

 util.get("getAllInfomationSort.do", null , function (response) {
	 if(response.status){
		 $scope.$apply(function () {
      $scope.sort = response.object;
	     
     	 });
	      for(var i=0;i<$scope.sort.length;i++)
	      {
	       if($scope.sort[i].remark.length>20)
	        $scope.sort[i].remark=$scope.sort[i].remark.substring(0,20)+"...";
	      }
	 }

      
     },function () {
     });
};
 /*获取分级*/
 $scope.getClassifi=function(){
  util.get("getAllInformationClassification.do",null
      ,function(response){      
       $scope.$apply(function () {
     $scope.classifi=response.object;
      });

      },function(){
      });
 };

/*获取来源渠道*/
$scope.getSource=function(){
	//alert();
 util.get("getAllInfomationSource.do",null
     ,function(response){
     	$scope.source=response.object;
      	var object=response.object;
      	jq("#sourcetitle").html("");
      	var titlehtml='<table id="treeTable1" class="table table-striped table-bordered" ></table>';
      	jq("#sourcetitle").append(titlehtml);
       	var html='<tr class="title"><th>编号</th><th>来源渠道名称</th><th>来源渠道备注</th></tr>'
		$("#treeTable1").html(html);
		childer=[];
		xxx(object,"#treeTable1");
      

     },function(){
     
     });
 //console.log("----------------");
};
/*获取分口信息*/
$scope.getFenkou=function(){
 util.get("getAllInformationPort.do",null//---------
     ,function(response){
	$scope.port=response.object;
     	var object=response.object;
	 if(response.status){
	 	jq("#porttitle").html("");
      	var titlehtml='<table id="treeTable2" class="table table-striped table-bordered" ></table>';
      	jq("#porttitle").append(titlehtml);
		 var html='<tr class="title"><th>编号</th><th>信息分口名称</th><th>信息分口备注</th></tr>';
      $("#treeTable2").html(html);    	
      childer2=[];      
      xxx(object,"#treeTable2");
     
	 }
     
     

     },function(){
     });
};
 $scope.getSource();
 $scope.getFenkou();
 /*初始化数据*/
$scope.getSort();
/*添加分类按钮*/
 $scope.addSort=function(){
  $scope.sortName="";
  $scope.sortReMark="";
  jq('#addSort').modal('show');
 }
/*保存添加分类按钮*/
 $scope.saveSort=function(){

  $scope.dataSort={};
 
 $scope.dataSort.name=jq("#sortName").val();

 $scope.dataSort.remark=jq("#sortReMark").val();

  $scope.strs=nameLength.strFun($scope.dataSort.name);

  if(!$scope.strs){
	  myTool.alert({		
			title:"提示",
			context:"请填写分类名称!",
			isMask:true,
	 });
   return;
  }
  util.post("addInfoSort.do",$scope.dataSort,function(response){
	  if(response.status){
	  	alert(2);
	  	//alert(1);
		  myTool.alert({		
				title:"提示",
				context:"添加分类成功!",
				isMask:true,
				callback:function(){
					 jq('#addSort').modal('hide');
					 $scope.getSort();
				}
		 });
	  }else{
		  myTool.alert({		
				title:"提示",
				context:"添加分类失败!",
				isMask:true,
				callback:function(){
					 
				}
		 });
	  }
  },function(){
  });
}
 /*添加分级按钮*/
 $scope.addClassfi=function(){
  $scope.classfiName="";
  $scope.classfiLevel="";
  jq('#addClassfi').modal('show');
 }
 /*保存添加分级按钮*/
 $scope.saveClassfi=function(){
  $scope.dataClassfi={};
  $scope.dataClassfi.name=jq("#classfiName").val();
  $scope.strs=nameLength.strFun($scope.dataClassfi.name);
  $scope.dataClassfi.level=jq("#classfiLevel").val();
  
  if(!$scope.strs){
	  myTool.alert({		
			title:"提示",
			context:"请填写分级名称!",
			isMask:true,
	 });
   return;
  }
  util.post("addInfoClassification.do",$scope.dataClassfi,function(response){
	  if(response.status){
		  myTool.alert({		
				title:"提示",
				context:"添加分级成功!",
				isMask:true,
				callback:function(){
					 jq('#addClassfi').modal('hide');
					 $scope.getClassifi();
				}
		 });
	  }else{
		  myTool.alert({		
				title:"提示",
				context:"添加分级失败!",
				isMask:true,
				callback:function(){
					
					 $scope.getClassifi();
				}
		 });
	  }
	 
 
  },function(){
   
  });
 }
 /*添加来源渠道按钮*/
 $scope.addSource=function(){
  $scope.sourceName="";
  $scope.sourceMark="";
  jq('#addSource').modal('show');
 }


 //公用方法


	function xxx(object,className){


		var a=[];
		var flag=0;
			var html="";
		var flaga={};
		
		function treeMenu(a){
		    this.tree=a||[];
		    this.groups={};
		};
		treeMenu.prototype={
		    init:function(superId){
		  
		        this.group();
		        return this.getDom(this.groups[superId]);
		    },
		    group:function(){
		    
		        for(var i=0;i<this.tree.length;i++){
		            if(this.groups[this.tree[i].superId]){
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }else{
		                this.groups[this.tree[i].superId]=[];
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }
		        }

		    },

		    getDom:function(a){
		    	
		    	
		        if(!a){return ''}
		       flag=0;
		        var html="";
		    
		    	if(a[0].superId==0)
		    	{
		    			for(var i=0;i<a.length;i++)
		    			{
		    				if(className=="#treeTable2")
		    				{
								html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td> </tr>\n';		
		    				
		    				}
		    				else
		    				{
		    					html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td></tr>\n';		
		    				}
		    				$(className).append(html);
		    				
		    			}
		    			
		    			flag=1;
		    	}
		    	if(flag==0)
		    	{
		    			var length=a.length;
		    			var superid=0;
		    			for(var i=0;i<length;i++)
		    			{	
		    				superid=a[i].superId;
		    				if(isChilde(a[i].id,object))
		    				{ 
		    					if(className=="#treeTable2")
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td> </tr>\n';		
		    				
		    					}
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td> </tr>\n';		
		    				
		    					}
		    				}  
		    				else
		    				{  		
		    					if(className=="#treeTable2")
		    					{
		    						html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td></tr>\n';		
		    					}
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td></tr>\n';		
		    					
		    					}
		    				}
		    				
		    				
		    				    
		    			}
		    				if(className=="#treeTable1")
		    				{
		    					childer.push({superid:superid,html:html});
		    				}
		    				else
		    				{
		    					childer2.push({superid:superid,html:html});
		    				}

		    			
		    		
		    		
		    			
		    	}
		    	 
		        for(var i=0;i<a.length;i++)
		        {
		           this.getDom(this.groups[a[i].id]);

		        }

	        /*html+='</optgroup>\n';*/

	       /* $("#"+id).append(html);*/
	        return html;
	    }
	   

	}
 var html=new treeMenu(object).init(0);
option(className);
}
	




//判断是否有子节点
function isChilde(id,object)
{
	for(var i=0;i<object.length;i++)
	{
		if(object[i].superId==id)
		{
			return true;
		}
	}
	return false;
}
	function option(className)
	{			
			var option = {
	                theme:'vsStyle',
	              
	                beforeExpand : function($treeTable, id) {

	            	var html="";
	                    //判断id是否已经有了孩子节点，如果有了就不再加载，这样就可以起到缓存的作用
	                    if ($('.' + id, $treeTable).length) { return; }
	            
	                   var index=jq(className).find("tr").eq(id).attr("id");
	                   if(className=="#treeTable2")
		    			{
		                   for(var i=0;i<childer2.length;i++)
		                   {
		                   	if(id==childer2[i].superid)
		                   	{
		                   		html=childer2[i].html;
		                   	}
		                   }
		                 }
		                 else
		                 {
		                 	 for(var i=0;i<childer.length;i++)
		                   	{
		                   		if(id==childer[i].superid)
		                   		{
		                   			html=childer[i].html;
		                   		}
		                  	 }
		                 }
	                
	                     $treeTable.addChilds(html);
					},
					 onSelect : function($treeTable, id) {
	                    window.console && console.log('onSelect:' + id);
	                    
	                }


		}	
	
		$(className).treeTable(option);
	}






 /*保存添加来源渠道按钮*/
 
 $scope.saveSource=function(){
  $scope.dataSource={};
  $scope.dataSource.name=jq("#sourceName").val();
  $scope.dataSource.remark=jq("#sourceMark").val();
 $scope.dataSource.superId=jq("#source-select").val();
 $scope.strs=nameLength.strFun($scope.dataSource.name);
  if(!$scope.strs){
	  myTool.alert({		
			title:"提示",
			context:"请填写渠道名称!",
			isMask:true,
	 });
   return;
  };
  util.post("addInfoSource.do",$scope.dataSource,function(response){
	  if(response.status){
		  myTool.alert({		
				title:"提示",
				context:"添加渠道成功!",
				isMask:true,
				callback:function(){
					jq('#addSource').modal('hide');
					 $scope.getSource();
				}
		 });
	  }else{
		  myTool.alert({		
				title:"提示",
				context:"添加渠道失败!",
				isMask:true,
				callback:function(){
					
					
				}
		 });
	  }
   
 
  },function(){
  });
 }
 $scope.getSort();
/*添加分口按钮*/
 $scope.addClassport=function(){
  $scope.classport="";
  $scope.classpo="";
  jq('#addPort').modal('show');
 }
/*保存添加分口按钮*/
 $scope.savePort=function(){
  $scope.dataPort={};
 $scope.dataPort.portName=jq("#classport").val();
 $scope.dataPort.remark=jq("#classpo").val();
 $scope.dataPort.superId=jq("#port-select").val();

  $scope.strs=nameLength.strFun($scope.dataPort.portName);
  if(!$scope.strs){
	  myTool.alert({		
			title:"提示",
			context:"请填写分口名称!",
			isMask:true,
	 });
   return;
  }
  util.post("addInformationPort.do",$scope.dataPort,function(response){
	  if(response.status){
		  myTool.alert({		
				title:"提示",
				context:"添加分口成功!",
				isMask:true,
				callback:function(){
					jq('#addPort').modal('hide');
					 $scope.getFenkou();
				}
		 });
	  }else{
		  myTool.alert({		
				title:"提示",
				context:"添加分口失败!",
				isMask:true,
				callback:function(){
					
					
				}
		 });
	  }
	 
		 
		 
	
   
  },function(){
  });
}
}]);
/*修改渠道控制器*/
 appController.controller("modifySourceCtrl",['$scope','$routeParams',function($scope,$routeParams){
		$scope.id="id="+$routeParams.sourceId;
		$scope.superId=0;
		$scope.level=0;
		util.get("getInfomationSourceById.do",$scope.id,function(response){
			if(response.status){
				$scope.source=response.object;
				 jq(".sourceName").val($scope.source.name);
				 jq(".sourceRemark").val($scope.source.remark);
				 $scope.superId=$scope.source.superId;
				 $scope.level=$scope.source.level;
				 util.get("getAllInfomationSource.do","null",function(response){
						if(response.status){
							
							 for(var i=0;i<response.object.length;i++){
								 if(response.object[i].id==$scope.source.superId){
									 var html= "<option selected='selected' value="+response.object[i].id+">"+response.object[i].name+"</option>";
									 jq(".source").append(html);
								 }else{
									 var html= "<option  value="+response.object[i].id+">"+response.object[i].name+"</option>";
									 jq(".source").append(html);
								 }
								 
							 }
							 
						}
					   
					},function(){
					});
			}
		   
		},function(){
		});
		$scope.source={};
		$scope.modifySource=function(){
			$scope.source.id=$routeParams.sourceId;
			 $scope.source.name=jq(".sourceName").val();
			 $scope.source.remark=jq(".sourceRemark").val();
			 $scope.source.superId=jq(".source option:selected").val();
			 $scope.source.level=$scope.level;
			 $scope.source.lastSuperId=$scope.superId;
			 if(jq(".sourceName").val()==""||jq(".sourceName").val()==null){
				 myTool.alert({		
						title:"提示",
						context:"请填写渠道名称!",
						isMask:true,
				 });
			 }else{
				 util.post("modifyInfoSource.do",$scope.source,function(response){
						if(response.status){
							 myTool.alert({		
									title:"提示",
									context:"修改信息渠道成功!",
									isMask:true,
							 });
						}else{
							 myTool.alert({		
									title:"提示",
									context:"修改信息渠道失败!",
									isMask:true,
							 });
						}
					   
					},function(){
						 myTool.alert({		
								title:"提示",
								context:"发生未知错误",
								isMask:true,
						 });
					});
			 }
			
		 }
		 
}]);

 /*修改分口控制器*/
 appController.controller("modifyPortCtrl",['$scope','$routeParams',function($scope,$routeParams){
		$scope.id="id="+$routeParams.portId;
		$scope.superId=0;
		$scope.level=0;
		util.get("getInfomationPortById.do",$scope.id,function(response){
			if(response.status){
				$scope.source=response.object;
				 jq(".portName").val($scope.source.portName);
				 jq(".portRemark").val($scope.source.remark);
				 $scope.superId=$scope.source.superId;
				 $scope.level=$scope.source.level;
				 util.get("getAllInformationPort.do","null",function(response){
						if(response.status){
							
							 for(var i=0;i<response.object.length;i++){
								 if(response.object[i].id==$scope.source.superId){
									 var html= "<option selected='selected' value="+response.object[i].id+">"+response.object[i].portName+"</option>";
									 jq(".port").append(html);
								 }else{
									 var html= "<option  value="+response.object[i].id+">"+response.object[i].portName+"</option>";
									 jq(".port").append(html);
								 }
								 
							 }
							 
						}
					   
					},function(){
					});
			}
		   
		},function(){
		});
		$scope.source={};
		$scope.modifyPort=function(){
			$scope.source.id=$routeParams.portId;
			 $scope.source.name=jq(".portName").val();
			 $scope.source.remark=jq(".portRemark").val();
			 $scope.source.superId=jq(".port option:selected").val();
			 $scope.source.level=$scope.level;
			 $scope.source.lastSuperId=$scope.superId;
			 if(jq(".portName").val()==""||jq(".portName").val()==null){
				 myTool.alert({		
						title:"提示",
						context:"请填写分口名称!",
						isMask:true,
				 });
			 }else{
				 util.post("modifyInformationPort.do",$scope.source,function(response){
						if(response.status){
							 myTool.alert({		
									title:"提示",
									context:"修改信息分口成功!",
									isMask:true,
							 });
						}else{
							 myTool.alert({		
									title:"提示",
									context:"修改信息分口失败!",
									isMask:true,
							 });
						}
					   
					},function(){
						 myTool.alert({		
								title:"提示",
								context:"发生未知错误",
								isMask:true,
						 });
					});
			 }
			
		 }
		 
}]);

/*
 修改信息设置控制器
 1.0 mouse 2016/07/19
 */
appController.controller("modifySortSetCtrl",['$scope','nameLength','isNaN',function($scope,nameLength,isNaN){
	 $scope.flag=0;



	 //公用方法


	function xxx(object,className){


		var a=[];
		var flag=0;
			var html="";
		var flaga={};
		
		function treeMenu(a){
		    this.tree=a||[];
		    this.groups={};
		};
		treeMenu.prototype={
		    init:function(superId){
		  
		        this.group();
		        return this.getDom(this.groups[superId]);
		    },
		    group:function(){
		    
		        for(var i=0;i<this.tree.length;i++){
		            if(this.groups[this.tree[i].superId]){
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }else{
		                this.groups[this.tree[i].superId]=[];
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }
		        }

		    },

		    getDom:function(a){
		    	
		    	
		        if(!a){return ''}
		       flag=0;
		        var html="";
		    
		    	if(a[0].superId==0)
		    	{
		    			for(var i=0;i<a.length;i++)
		    			{
		    				var iiy=i+1;
		    				if(className=="#treeTable2")
		    				{
								html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="portId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update portName" value="'+a[i].portName+'"/></td><td> <input type="text" readonly="readonly" class="date-update portRemark" value="'+a[i].remark+'"/></td><td class="row"><a href="#modifyPort/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifyPortBtn" >修改</button></a>'+
                                       ' <button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1 delPortBtn" data-attrid='+iiy+'>删除</button></td> </tr>\n';		
		    				
		    				}
		    				else
		    				{
		    					html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="sourceId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update sourceName" value="'+a[i].name+'"/></td><td><input type="text" readonly="readonly" class="date-update sourceRemark" value="'+a[i].remark+'"/></td><td><a href="#modifySource/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifySourceBtn" >修改</button></a>'+
                                        '<button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1 delSourceBtn" data-attrid='+iiy+'>删除</button></td> </tr>\n';		
		    				}
		    				$(className).append(html);
		    				
		    			}
		    			
		    			flag=1;
		    	}
		    	if(flag==0)
		    	{
		    			var length=a.length;
		    			var superid=0;
		    			for(var i=0;i<length;i++)
		    			{	
		    				superid=a[i].superId;
		    				if(isChilde(a[i].id,object))
		    				{ 
		    						var iiy=i+1;
		    					if(className=="#treeTable2")
		    					{

		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="portId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update portName" value="'+a[i].portName+'"/></td><td> <input type="text" readonly="readonly" class="date-update portRemark" value="'+a[i].remark+'"/></td><td class="row"><a href="#modifyPort/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifyPortBtn" >修改</button></a>'+
                                       ' <button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1  delPortBtn" data-attrid='+iiy+'>删除</button></td> </tr>\n';		
	
		    					}
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="sourceId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update sourceName" value="'+a[i].name+'"/></td><td><input type="text" readonly="readonly" class="date-update sourceRemark" value="'+a[i].remark+'"/></td><td><a href="#modifySource/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifySourceBtn" >修改</button></a>'+
                                        '<button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1 delSourceBtn" data-attrid='+iiy+'>删除</button></td> </tr>\n';		
		    				
		    					}
		    				}  
		    				else
		    				{  		
		    						var iiy=i+1;
		    					if(className=="#treeTable2")
		    					{
		    						html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="portId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update portName" value="'+a[i].portName+'"/></td><td> <input type="text" readonly="readonly" class="date-update portRemark" value="'+a[i].remark+'"/></td><td class="row"><a href="#modifyPort/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifyPortBtn" >修改</button></a>'+
                                       ' <button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1  delPortBtn" data-attrid='+iiy+'>删除</button></td> </tr>\n';	
                                 }	
	
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td class="hide"><input class="sourceId" value='+a[i].id+'></td><td><input type="text" readonly="readonly" class="date-update sourceName" value="'+a[i].name+'"/></td><td><input type="text" readonly="readonly" class="date-update sourceRemark" value="'+a[i].remark+'"/></td><td><a href="#modifySource/'+a[i].id+'"><button class="btn btn-primary btn-xs col-xs-3 modifySourceBtn" >修改</button></a>'+
                                        '<button class="btn btn-primary btn-xs col-xs-3 col-xs-offset-1 delSourceBtn" data-attrid='+iiy+'>删除</button></td></tr>\n';		
		    					
		    					}
		    				}
		    				
		    				
		    				    
		    			}
		    				if(className=="#treeTable1")
		    				{
		    					childer.push({superid:superid,html:html});
		    				}
		    				else
		    				{
		    					childer2.push({superid:superid,html:html});
		    				}

		    			
		    		
		    		
		    			
		    	}
		    	 
		        for(var i=0;i<a.length;i++)
		        {
		           this.getDom(this.groups[a[i].id]);

		        }

	        /*html+='</optgroup>\n';*/

	       /* $("#"+id).append(html);*/
	        return html;
	    }
	   

	}
 var html=new treeMenu(object).init(0);
option(className);
}
	




//判断是否有子节点
function isChilde(id,object)
{
	for(var i=0;i<object.length;i++)
	{
		if(object[i].superId==id)
		{
			return true;
		}
	}
	return false;
}
	function option(className)
	{			
			var option = {
	                theme:'vsStyle',
	              
	                beforeExpand : function($treeTable, id) {

	            	var html="";
	                    //判断id是否已经有了孩子节点，如果有了就不再加载，这样就可以起到缓存的作用
	                    if ($('.' + id, $treeTable).length) { return; }
	            
	                   var index=jq(className).find("tr").eq(id).attr("id");
	                   if(className=="#treeTable2")
		    			{
		                   for(var i=0;i<childer2.length;i++)
		                   {
		                   	if(id==childer2[i].superid)
		                   	{
		                   		html=childer2[i].html;
		                   	}
		                   }
		                 }
		                 else
		                 {
		                 	 for(var i=0;i<childer.length;i++)
		                   	{
		                   		if(id==childer[i].superid)
		                   		{
		                   			html=childer[i].html;
		                   		}
		                  	 }
		                 }
	                
	                     $treeTable.addChilds(html);
					},
					 onSelect : function($treeTable, id) {
	                    window.console && console.log('onSelect:' + id);
	                    
	                }


		}	
	
		$(className).treeTable(option);
	}



	 /*获取分类*/
	 $scope.getSort=function(){
	  util.get("getAllInfomationSort.do", null
	      , function (response) {
	       $scope.$apply(function () {
     	 $scope.sort = response.object;
     	 	});
	      }, function () {
	      });
	 };
	 /*获取分级*/
	 $scope.getClassifi=function(){
	  util.get("getAllInformationClassification.do",null
	      ,function(response){
	       $scope.$apply(function () {
     	 $scope.classifi = response.object;
     	 	});
	      },function(){
	      });
	 };
	 var childer2=[];
	var childer=[];
	 /*获取分口信息*/
	$scope.getFenkou=function(){
		//alert();
	 util.get("getAllInformationPort.do",null,function(response){
	      
     	 $scope.infoPort = response.object;
     	 var object=response.object;
     	 jq("#porttitle").html("");
      	var titlehtml='<table id="treeTable2" class="table table-striped table-bordered" ></table>';
      	jq("#porttitle").append(titlehtml);
       	var html='<tr class="title"><th>编号</th><th>信息分口名称</th><th>信息分口备注</th><th>操作</th></tr>'
		$("#treeTable2").html(html);
		childer2=[];
		xxx(object,"#treeTable2");
	     },function(){
	     });
	};
	
	 /*获取来源渠道*/
	 $scope.getSource=function(){
	  util.get("getAllInfomationSource.do",null
	      ,function(response){
	   
     	 $scope.source = response.object;
     	 var object=response.object;
     	 jq("#sourcetitle").html("");
      	var titlehtml='<table id="treeTable1" class="table table-striped table-bordered" ></table>';
      	jq("#sourcetitle").append(titlehtml);
       	var html='<tr class="title"><th>编号</th><th>来源渠道名称</th><th>来源渠道备注</th><th>操作</th></tr>'
		$("#treeTable1").html(html);
		childer=[];
		xxx(object,"#treeTable1");

	      },function(){
	      });
	 };
	 /*初始化数据*/
	 $scope.getSort();
	 $scope.getSource();
	$scope.getFenkou();
	 /*修改分类*/
	 $scope.modifySortBtn=function(index){
		 
	  if(jq(".modifyBtn").eq(index).text()=="修改")
	  {
	   jq(".modifyBtn").eq(index).text("保存");
	   jq(".sortRemark").eq(index).removeAttr('readonly');
	   jq(".sortRemark").eq(index).addClass("date-ok");
	   jq(".sortName").eq(index).removeAttr('readonly');
	   jq(".sortName").eq(index).addClass("date-ok");
	  }
	  else
	  {
		 
	   jq(".modifyBtn").eq(index).text("修改");
	   $scope.modifySort={};
	   $scope.modifySort.id=jq(".sortId").eq(index).val();
	   $scope.modifySort.name=jq(".sortName").eq(index).val();
	   
	   $scope.modifySort.remark=jq(".sortRemark").eq(index).val();
	   $scope.strs=nameLength.strFun($scope.modifySort.name);
	  
	   if(!$scope.strs){
		   myTool.alert({		
				title:"提示",
				context:"请填写分类名称!",
				isMask:true,
				callback:function(){
				$scope.flag=1;	
					
				}
		 });
	   return;
	  };
	   //修改数据 点击保存后，将数据传值后台
	   util.post("modifyInfoSort.do",$scope.modifySort,function(resposne){
		   if(resposne.status){
			   myTool.alert({		
					title:"提示",
					context:"修改成功!",
					isMask:true,
					callback:function(){
						$scope.flag=0;
					}

			 });
		   }else {
			   myTool.alert({		
					title:"提示",
					context:"修改失败!",
					isMask:true,
					callback:function(){
						
					}
			 });
		   }
	   
	    $scope.getSort();
	   },function(){
	   })
	   jq(".sortRemark").eq(index).attr('readonly','readonly');
	   jq(".sortRemark").removeClass("date-ok");
	   jq(".sortName").eq(index).attr('readonly','readonly');
	   jq(".sortName").removeClass("date-ok");
	  }

	 };
	 /*修改分级*/
	 $scope.modifyClassifBtn=function(index) {

	  if (jq(".modifyClassifiBtn").eq(index).text() == "修改") {
	   jq(".modifyClassifiBtn").eq(index).text("保存");
	   jq(".classLevel").eq(index).removeAttr('readonly');
	   jq(".classLevel").eq(index).addClass("date-ok");
	   jq(".classiName").eq(index).removeAttr('readonly');
	   jq(".classiName").eq(index).addClass("date-ok");
	  }
	  else {
	   jq(".modifyClassifiBtn").eq(index).text("修改");
	   $scope.modifyClassif = {};
	   $scope.modifyClassif.id = jq(".classifId").eq(index).val();
	   $scope.modifyClassif.name = jq(".classiName").eq(index).val();
	   $scope.modifyClassif.level = jq(".classLevel").eq(index).val();
	   $scope.strs=nameLength.strFun($scope.modifyClassif.name);
	 
	   if(!$scope.strs){
		   myTool.alert({		
				title:"提示",
				context:"请填写分级名称!",
				isMask:true,
				callback:function(){
					
					
				}
		 });
	   return;
	    };
	    
	   //修改数据 点击保存后，将数据传值后台
	   util.post("modifyInfoClassification.do", $scope.modifyClassif, function (resposne) {
		   if(resposne.status){
			   myTool.alert({		
					title:"提示",
					context:"修改成功!",
					isMask:true,
					callback:function(){
						
					}
			 });
		   }else {
			   myTool.alert({		
					title:"提示",
					context:"修改失败!",
					isMask:true,
					callback:function(){
						
					}
			 });
		   }
	    $scope.getClassifi();
	   }, function () {
	   })
	   jq(".classLevel").eq(index).attr('readonly', 'readonly');
	   jq(".classLevel").removeClass("date-ok");
	   jq(".classiName").eq(index).attr('readonly', 'readonly');
	   jq(".classiName").removeClass("date-ok");
	  }
	 }
	  /*修改来源渠道*/
	  $scope.modifySourcBtn=function(index){

	   if(jq(".modifySourceBtn").eq(index).text()=="修改")
	   {
	    jq(".modifySourceBtn").eq(index).text("保存");
	    jq(".sourceRemark").eq(index).removeAttr('readonly');
	    jq(".sourceRemark").eq(index).addClass("date-ok");
	    jq(".sourceName").eq(index).removeAttr('readonly');
	    jq(".sourceName").eq(index).addClass("date-ok");
	   }
	   else
	   {
	    jq(".modifySourceBtn").eq(index).text("修改");
	    $scope.modifySource={};
	    $scope.modifySource.id=jq(".sourceId").eq(index).val();
	    $scope.modifySource.name=jq(".sourceName").eq(index).val();
	    $scope.modifySource.remark=jq(".sourceRemark").eq(index).val();
	    $scope.strs=nameLength.strFun($scope.modifySource.name);
	    if(!$scope.strs){
	    	myTool.alert({		
				title:"提示",
				context:"请填写渠道名称!",
				isMask:true,
				callback:function(){
					
					
				}
		 });
	    return;
	    } ;
	    //修改数据 点击保存后，将数据传值后台
	    util.post("modifyInfoSource.do",$scope.modifySource,function(resposne){
	    	if(resposne.status){
				   myTool.alert({		
						title:"提示",
						context:"修改成功!",
						isMask:true,
						callback:function(){
							
						}
				 });
			   }else {
				   myTool.alert({		
						title:"提示",
						context:"修改失败!",
						isMask:true,
						callback:function(){
							
						}
				 });
			   }
	     $scope.getSource();
	    },function(){
	    })
	    jq(".sourceRemark").eq(index).attr('readonly','readonly');
	    jq(".sourceRemark").removeClass("date-ok");
	    jq(".sourceName").eq(index).attr('readonly','readonly');
	    jq(".sourceName").removeClass("date-ok");
	   }
	 };

	   /*修改分口*/
	  $scope.modifyPortBtn=function(index){

	   if(jq(".modifyPortBtn").eq(index).text()=="修改")
	   {
	    jq(".modifyPortBtn").eq(index).text("保存");
	    jq(".portRemark").eq(index).removeAttr('readonly');
	    jq(".portRemark").eq(index).addClass("date-ok");
	    jq(".portName").eq(index).removeAttr('readonly');
	    jq(".portName").eq(index).addClass("date-ok");
	   }
	   else
	   {
	    jq(".modifyPortBtn").eq(index).text("修改");
	    $scope.modifyPort={};
	    $scope.modifyPort.id=jq(".portId").eq(index).val();
	    $scope.modifyPort.portName=jq(".portName").eq(index).val();
	    $scope.modifyPort.remark=jq(".portRemark").eq(index).val();
	    $scope.strs=nameLength.strFun($scope.modifyPort.portName);
	    if(!$scope.strs){
	    	myTool.alert({		
				title:"提示",
				context:"请填写分口名称!",
				isMask:true,
				callback:function(){
					
					
				}
		 });
	    return;
	    } ;
	    //修改数据 点击保存后，将数据传值后台
	    //--------------------------------
	    util.post("modifyInformationPort.do",$scope.modifyPort,function(response){
	    	if(response.status){
				   myTool.alert({		
						title:"提示",
						context:"修改失败!",
						isMask:true,
						callback:function(){
							
						}
				 });
			   }else {
				   myTool.alert({		
						title:"提示",
						context:"修改成功!",
						isMask:true,
						callback:function(){
							
						}
				 });
			   }
	     $scope.getFenkou();
	    },function(){
	    })
	    jq(".portRemark").eq(index).attr('readonly','readonly');
	    jq(".portRemark").removeClass("date-ok");
	    jq(".portName").eq(index).attr('readonly','readonly');
	    jq(".portName").removeClass("date-ok");
	   }
	 };
	 /*删除分口*///----------------------
	$scope.delPortBtn=function(index){

	  $scope.portId=index;
	 jq("#delPortModal").modal('show');
	}
	 $scope.deletePort=function(){
	 $scope.deleSort={};

	  $scope.deleSort.id=jq(".portId").eq($scope.portId-1).val();
	
	  //-------------------------------------
	  util.post("deleteInformationPort.do",$scope.deleSort,function(){
	 
	   jq("#delPortModal").modal('hide');
	   $scope.getFenkou();
	  },function(){
	  })
	 }

	 /*删除分类*/
	$scope.delSortBtn=function(index){
	  $scope.sortId=index;
	 jq("#delSortModal").modal('show');
	}
	 $scope.deleteSort=function(){
	 $scope.deleSort={};
	  $scope.deleSort.id=jq(".sortId").eq($scope.sortId).val();
	  util.post("delInfoSort.do",$scope.deleSort,function(){
	  
	   jq("#delSortModal").modal('hide');
	   $scope.getSort();
	  },function(){
	  })
	 }


	 /*删除分级*/
	$scope.delClassifBtn=function(index){
	  $scope.classifId=index;
	 jq("#delClassifModal").modal('show');
	}
	 $scope.deleClassif=function(){
	  $scope.deleClassi={};
	  $scope.deleClassi.id=jq(".classifId").eq($scope.classifId).val();
	  util.post("delInfoClassification.do",$scope.deleClassi,function(){
	  
	   $scope.getClassifi();
	   jq("#delClassifModal").modal('hide');
	  },function(){
	  })
	 }
	 /*删除来源渠道*/
	 $scope.delSourceBtn=function(index){
	 
	  $scope.sourceId=index-1;
	 jq("#delSourceModal").modal('show');
	}
	 $scope.deleteSource=function(){
	 
	  $scope.deleSource={};
	  $scope.deleSource.id=jq(".sourceId").eq($scope.sourceId).val();
	  
	  
	  util.post("delInfoSource.do",$scope.deleSource,function(){
	
	   $scope.getSource();
	   jq("#delSourceModal").modal('hide');
	  },function(){
	  })
	 }


	 jq("#deleteSource").click(function(){
	 	$scope.deleteSource();
	 })
	 jq("#deletePort").click(function(){
	 	$scope.deletePort();
	 })

	  jq(document).on('click','.delPortBtn',function(){
	 	var attrid=$(this).attr("data-attrid");
	 	var attrid=jq("#treeTable2").find("tr").index($(this).parents("tr"));
	 
	 	$scope.delPortBtn(attrid);
	 })
	  jq(document).on('click','.delSourceBtn',function(){
	 	var attrid=$(this).attr("data-attrid");

	 	var attrid=jq("#treeTable1").find("tr").index($(this).parents("tr"));
	 
	 	$scope.delSourceBtn(attrid);
	 })
	}])
/*
 添加职务控制器
 1.0 mouse 2016/08/27
*/
appController.controller("addPostCtrl",['$scope',function($scope){
	
	$scope.structure=1;
	//请求所有部门
	util.get("getDepartmentAndArea.do",null,function(response){
	    $scope.$apply(function () {
      $scope.departments = response.object;
	     
     	 });
	},function(){
	});
	jq(".department").change(function(){
		jq(".child").html("");
		str='<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		var structureId = jq(this).val();
		var structureId = jq(this).val();
		if( structureId !="choice"){
			if(jq(".department :selected").attr("data-num")=='0'){
				util.get("getAllStructureBySuperId.do",{superId:structureId},function(data){
					var str="";
					var object=data.object;
					jq(".child").html("");
					str='<option value="choice">请选择分监区</option>';
					$(".child").append(str);
					for(var i=0;i<object.length;i++){
						str='<option value="'+object[i].id+'">'+object[i].name+'</option>';
						jq(".child").append(str);	
					}
					
				},function(){
					
				});
			}
		}
	});
	//创建
	$scope.createModel=function(){
	   	if(jq(".positionName").val()==""||jq(".positionName").val()==null){
	   		myTool.alert({		
	   			title:"提示",
	   			context:"职务名称不能为空！",
	   			isMask:true,
	   			callback:function(){
	   			    
	   			}
	   		});
	   	}else{
	   		jq("#createRole").modal("show");
	   	}
   }
	//创建职务
	  $scope.addPosition=function(){
		  /*var structureId;
		  var structureName;
		  if(jq(".child").val()!="choice"){
			 
			  structureId=jq(".child :selected").val();
			  structureName=jq(".child :selected").text();
		  }else if(jq(".department").val()!="choice"){
			 
			 structureId=jq(".department :selected").val();
			 structureName=jq(".department :selected").text();
		  }else{
			 
			  structureId=jq(".structure :selected").val();
			  structureName=jq(".structure :selected").text();
		  }*/
		    jq("#createRole").modal("hide");
		    
		    $scope.newPosition={};
		    
		    $scope.newPosition.positionName=jq(".positionName").val();
		    $scope.newPosition.remark=jq(".remark").val();
		    util.get("addPosition.do", $scope.newPosition,function(re){
		             if(re.status){
		              jq("#sucessModel").modal('show');
		              jq(".positionName").val("");
		              jq(".remark").val("");
		             }else{
		             }
		             
		            },function(){
		             
		            });
		   
		    };
}]);
/*
职务列表控制器
 1.0 mouse 2016/08/27
*/
appController.controller("postListCtrl",['$scope',function($scope){
	
	util.get("getAllPsoition.do",$scope.structureId,function(response){
	    $scope.$apply(function(){
	    	$scope.positions = response.object;
	    })
    	
	     
	},function(){
		
	});

//	util.get("getDepartmentAndArea.do",null,function(response){
//		$scope.$apply(function () {
//    $scope.departments = response.object;
//	     
//     	 });
//		$scope.getPosition(1);
//	},function(){
//	});
//	jq(".structure").click(function() {
//		var superId = $(this).val();
//		$scope.getPosition(superId);
//	});
//	jq(".department").change(function(){
//		jq(".child").html("");
//		str='<option value="choice">请选择分监区</option>';
//		$(".child").append(str);
//		var structureId = jq(this).val();
//		if( structureId !="choice"){
//			$scope.getPosition(structureId);
//			if(jq(".department :selected").attr("data-num")=='0'){
//				util.get("getAllStructureBySuperId.do",{superId:structureId},function(data){
//					var str="";
//					var object=data.object;
//					jq(".child").html("");
//					str='<option value="choice">请选择分监区</option>';
//					$(".child").append(str);
//					for(var i=0;i<object.length;i++){
//						str='<option value="'+object[i].id+'">'+object[i].name+'</option>';
//						jq(".child").append(str);	
//					}
//					
//				},function(){
//					
//				});
//			}
//		}
//	});
//	$(".child").change(function(){
//		var structureId = $(this).val();
//		if( structureId !="choice"){
//			$scope.getPosition(structureId);
//		}
//	});
	 /*删除职务*/
	 $scope.delBtn=function(index){
		 $scope.delR=index;
		 jq("#delModal").modal('show');
	 }
	$scope.deleteRoleBtn=function(){
		jq("#delModal").modal('hide');
		$scope.delePosition={};
		$scope.delePosition.id=jq(".positionId").eq($scope.delR).val();
		util.post("deletePosition.do",$scope.delePosition,function(){
			$scope.getPosition();
		
	},function(){
	  
	 });
	};
	$scope.getPosition=function(){
		util.get("getAllPsoition.do",$scope.structureId,function(response){
//		    
//	    	$scope.positions = response.object;
//		     
		},function(){
//		    
		});
	}
	
	
//	$scope.getPosition=function(structureId){
//		$scope.structureId="structureId="+structureId;
//		util.get("getAllPsoitionByStructureId.do",$scope.structureId,function(response){
//		    
//		    $scope.$apply(function () {
//	     $scope.positions = response.object;
//		     
//	     	 });
//		},function(){
//		});
//	};
	
}]);
/*
修改职务控制器
 1.0 mouse 2016/08/27
*/
appController.controller("modifyPostCtrl",['$scope','$routeParams',function($scope,$routeParams){
	$scope.id="id="+$routeParams.positionId;
	util.get("getPositionById.do",$scope.id,function(response){
		if(response.status){
			$scope.position=response.object;
			 jq(".positionName").val($scope.position.positionName);
			 jq(".positionRemark").val($scope.position.remark);
		}
	   
	},function(){
	});
	$scope.position={};
	$scope.modifyModel=function(){
		$scope.position.id=$routeParams.positionId;
		 $scope.position.positionName=jq(".positionName").val();
		 $scope.position.remark=jq(".positionRemark").val();

		 if(jq(".positionName").val()==""||jq(".positionName").val()==null){
			 myTool.alert({		
					title:"提示",
					context:"请填写职务名称!",
					isMask:true,
			 });
			 return false;
		 }
		  jq("#modifyPost").modal('show');
	 }
	 $scope.modifyPost=function(){
		util.post("modifyPosition.do",$scope.position,function(re){
			  if(re.status){
				  myTool.alert({		
						title:"提示",
						context:"修改职务成功!",
						isMask:true,
				 });
				  jq("#modifyPost").modal('hide');
			     
			  }else{
				  myTool.alert({		
						title:"提示",
						context:"修改职务失败!",
						isMask:true,
				 });
			   }
			  },function(){
			   
			  });
	 }
}]);
/*
 添加角色及权限控制器
 1.0 mouse 2016/07/19
 */
appController.controller("addRoleCtrl",['$scope','strNull',function($scope,strNull){
  $scope.powerRole={};
  $scope.optival=1;
  
  util.get("getInfomationPortBysuperId.do",{superId:0},function(response){
	  if(response.status){
    	  $scope.InformationPort=response.object;
//    	  var html='<div class="col-xs-6 "> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[0].id+'" />'+$scope.InformationPort[0].portName+'</div>';
//    	  '<div class="col-xs-6 "> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[1].id+'" />'+$scope.InformationPort[1].portName+'</div>';
//    	  jq(".rolePower").append(html);
//      }
		  $scope.InformationPort=response.object;
	      for(var i=0;i<$scope.InformationPort.length;i+=3){
	    	  if(i>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  var html='<div class="col-xs-2 col-xs-offset-2"> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i].id+'" />'+$scope.InformationPort[i].portName+'</div>';
	    	  jq(".rolePower").append(html);
	    	  if(i+1>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  html=' <div class="col-xs-2 "><input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i+1].id+'" />'+$scope.InformationPort[i+1].portName+'</div>';
	    	  jq(".rolePower").append(html);
	    	  if(i+2>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  html=' <div class="col-xs-6 "><input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i+2].id+'" />'+$scope.InformationPort[i+2].portName+'</div>';
	    	  jq(".rolePower").append(html);
	      }
	  }
     },function(){
  });
  $scope.addRole=function(){
    jq("#createRole").modal("hide");
    $scope.roleStructure={};
    
    $scope.roleStructure.id=$scope.optival;
    $scope.newRole={};
    $scope.newRole.roleName=jq(".roleName").val();
    $scope.newRole.remark=jq(".roleRemark").val();
    $scope.newRole.type=jq(".roleType :selected").val();
    $scope.newRole.prisonStructure=$scope.roleStructure;
    var portCheck=new Array();
    jq(".portPower:checked").each(function(){
    	portCheck.push(jq(this).attr("data-id"));
    })
    $scope.portStr=portCheck.join(",");
    
    $scope.rid=strNull.strNu($scope.roleStructure.id);
    $scope.rname=strNull.strNu($scope.newRole.roleName);
   
    util.post("addPrisonRole.do",{pRoleStr:JSON.stringify($scope.newRole),powerRoleStr:JSON.stringify($scope.powerRole), portStr:$scope.portStr},function(re){
       
             if(re.status){
              jq("#sucessModel").modal('show');
              jq(".roleName").val("");
              jq(".roleRemark").val("");
            
             }else{
             }
             
            },function(){
             
            });
    };

    $scope.createModel=function(){
    	 if(jq(".roleName").val()==""||jq(".roleName").val()==null){
    		myTool.alert({		
    			title:"提示",
    			context:"角色名称不能为空！",
    			isMask:true,
    			callback:function(){
    			    
    			}
    		});
    	}else{
    		jq("#createRole").modal("show");
    	}
    	
    }
  }
]);


/*
 角色列表控制器
 1.0 mouse 2016/07/22
 */
appController.controller("roleListCtrl",['$scope',function($scope){
 /*数据初始化*/
 $scope.role={};
 $scope.optival=1;
 /*获取部门，根据部门查询*/
 $scope.getstru=function(){
 util.get("getAllRole.do","null",function(response){
    	$scope.$apply(function(){
    		$scope.role=response.object;
    	})
	  
     },function(){
     });
 };

 $scope.getstru();



 /*删除角色*/
 $scope.delBtn=function(index){
  $scope.delR=index;
  jq("#delModal").modal('show');
 }
$scope.deleteRoleBtn=function(){
  jq("#delModal").modal('hide');
 $scope.deleRole={};
 $scope.deleRole.id=jq(".roleId").eq($scope.delR).val();
 util.post("delPrisonRole.do",$scope.deleRole,function(){
	 util.get("getAllRole.do","null",function(response){
		    $scope.$apply(function(){
    			  $scope.role=response.object;
    		})
		
	     },function(){
	     });
 },function(){
  
 });
};

 /*根据部门查询*/
// $scope.serchRole=function(){
//  $scope.structureId="";
//  $scope.structureId="structureId="+$scope.optival;
//  util.post("getRoleByStructureId.do",$scope.structureId,function(response){
//  $scope.role=response.object;
//  },function(){
//  });
// };
}]);



/*
 修改角色控制器
 1.0 mouse 2016/07/23
 */
appController.controller("roleModifyCtrl",['$scope','$routeParams','strNull',function($scope,$routeParams,strNull){

 /*获取所有部门*/
 $scope.powercheck={};
 $scope.roleId="roleId="+$routeParams.roleId;
  util.post("getPowerByRoleId.do",$scope.roleId,function(response){
  $scope.powercheck=response.object[0];
  },function(){
  });
  util.post("getPortPowerByRoleId.do",$scope.roleId,function(response){
	 if(response.status){
			 var idStr=response.object;
			 if(response.object==null){
				
			 }else{
				 
					setTimeout(function(){
						$("input[class='portPower']").each(function(){
							for(var i=0;i<idStr.length;i++){
								if($(this).attr("data-id")==idStr[i].portId){
									$(this).attr("checked","checked");
									break;
								}
							}
							
						});
					},10);
			
			 }
			 
	}
		
	 
  },function(){
  });
  util.get("getInfomationPortBysuperId.do",{superId:0},function(response){
      if(response.status){
    	  $scope.InformationPort=response.object;
//    	  var html='<div class="col-xs-6 "> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[0].id+'" />'+$scope.InformationPort[0].portName+'</div>';
////    	  '<div class="col-xs-6 "> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[1].id+'" />'+$scope.InformationPort[1].portName+'</div>';
//
//    	  jq(".rolePower").append(html);
	      for(var i=0;i<$scope.InformationPort.length;i+=3){
	    	  if(i>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  var html='<div class="col-xs-2 col-xs-offset-2"> <input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i].id+'" />'+$scope.InformationPort[i].portName+'</div>';
	    	  jq(".rolePower").append(html);
	    	  if(i+1>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  html=' <div class="col-xs-2 "><input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i+1].id+'" />'+$scope.InformationPort[i+1].portName+'</div>';
	    	  jq(".rolePower").append(html);
	    	  if(i+2>=$scope.InformationPort.length){
	    		  break;
	    	  }
	    	  html=' <div class="col-xs-6 "><input type="checkbox" class="portPower"  data-id="'+ $scope.InformationPort[i+2].id+'" />'+$scope.InformationPort[i+2].portName+'</div>';
	    	  jq(".rolePower").append(html);
	      }
      }
     },function(){
  });
// util.get("getAllStructure.do",null
//     ,function(response){
//      $scope.obj=response.object;
//      /*jq("#select_id").find("option").eq(2).attr('selected','selected');*/
//     },function(){
//     });
// 

 /*根据id查找*/
 $scope.prisonRole={};
 $scope.strue={};
 $scope.selectname="";

 $scope.id="id="+$routeParams.roleId;
  util.post("getPrisonRoleById.do",$scope.id,function(response){
           $scope.prisonRole=response.object;
//           $scope.strue=$scope.prisonRole.prisonStructure;
           $scope.selectname=$scope.strue.name;
          },function(){
          });

 $scope.roleId="roleId="+$routeParams.roleId;


 $scope.renderFinish = function(d){
    if(d){
     $scope.getsafy();
    }
 }

 $scope.newRole={};
 /*修改角色提示*/
 $scope.modifyModel=function(){

//  $scope.roleStructure={};
//  for(var i=0;i<$scope.obj.length;i++){
//   if($scope.selectname==$scope.obj[i].name){
//    $scope.roleStructure.id=$scope.obj[i].id;
//   }
//  };
  
  $scope.newRole.id=$routeParams.roleId;
   $scope.newRole.roleName=jq(".roleName").val();
  $scope.newRole.remark=$scope.prisonRole.remark;
//  $scope.newRole.prisonStructure=$scope.roleStructure;
//  $scope.rid=strNull.strNu($scope.roleStructure.id);
  $scope.rname=strNull.strNu($scope.newRole.roleName);
//  if(!$scope.rid){
//    jq("#badmodal").modal('show');
//    return false;
//  };
  if(!$scope.rname){
    jq("#badmodal").modal('show');
    return;
  }
  jq("#moeifyRole").modal('show');
 }
/*修改角色*/
 $scope.modifyRole=function(){
	 var portCheck=new Array();
	    jq(".portPower:checked").each(function(){
	    	portCheck.push(jq(this).attr("data-id"));
	    })
	    $scope.portStr=portCheck.join(",");
	    
  util.post("modifyePrisonRole.do",{pRole:JSON.stringify($scope.newRole),powerRoleStr:JSON.stringify($scope.powercheck),portStr:$scope.portStr},function(re){

   if(re.status){
	  jq("#moeifyRole").modal('hide');
      jq("#moeifySucess").modal('show');
     
   }else{

   }
   
  },function(){
  });
 };
}]);





/***********************************************************************************************************/
/*
 分口排序控制器
 1.0 mouse 2016/07/19
 */
appController.controller("sortfenkouCtrl",['$scope','nameLength','isNaN',function($scope,nameLength,isNaN){
	/*获取一级来源渠道*/
/*"getInfomationSourceBysuperId.do"*/
var childer2=[];//分口
var childer=[];//渠道
$scope.addinfo=function()
{
	getSource();//数据初始化
	getFenkou();//数据初始化
	upAndDown();//上移下移
}
$scope.addinfo();
function getSource(){
	var object=[];
	util.get("getAllInfomationSource.do","null",function(response)
	{
	       
		object = response.object;
	  	var html='<tr class="title"><th>编号</th><th>来源渠道名称</th><th>来源渠道备注</th><th>优先级</th><th>排序</th></tr>'
		$("#treeTable1").html(html);childer=[];
		xxx(object,"#treeTable1");
	},function(){

	});
	
			
			


}

//分口点击后

function getFenkou(){
	var object=[];
	util.get("getAllInformationPort.do","null",function(response)
	{
	       
	  object = response.object;
	  var html='<tr class="title"><th>编号</th><th>信息分口名称</th><th>信息分口备注</th><th>优先级</th><th>排序</th></tr>';
      $("#treeTable2").html(html);    	
      childer2=[];      
      xxx(object,"#treeTable2");
	},function(){

	});
	
			
		


}
	
$("#saveFirstSource").click(function(){

	var Information={};
	Information.idStr="";
	Information.levelStr="";
	$("#treeTable1").find("tr").each(function(e){
		if($(this).attr("isChange")=="true")
		{
			var id=$(this).attr("id");
			var level=$(this).find("td").eq(3).html();
			Information.levelStr+=level+",";
			Information.idStr+=id+",";

		}

	})

	util.get("infoSoureCompositor.do",Information,function(response)
		{//保存渠道---------------------------------------------------------------
		
			if(response.status)
			{
				jq("#sucessModel").modal('show');
			}
	    },function()
	    {

	    });


})

$("#saveFirstFenkou").click(function(){
	var Information={};
	Information.idStr="";
	Information.levelStr="";
	
	$("#treeTable2").find("tr").each(function(e){
		if($(this).attr("isChange")=="true")
		{
			var id=$(this).attr("id");
			var level=$(this).find("td").eq(3).html();
			var html={id:id,level:level};
			Information.levelStr+=level+",";
			Information.idStr+=id+",";

		}

	})
	console.log(JSON.stringify(Information));
	util.get("infoPortCompositor.do",Information,function(response)
		{//保存分口----------------------------------------------------------------------
			if(response.status)
			{
				jq("#sucessModel").modal('show');
			}
	    },function()
	    {

	    });


})
		

	function xxx(object,className){


		var a=[];
		var flag=0;
			var html="";
		var flaga={};
		
		function treeMenu(a){
		    this.tree=a||[];
		    this.groups={};
		};
		treeMenu.prototype={
		    init:function(superId){
		  
		        this.group();
		        return this.getDom(this.groups[superId]);
		    },
		    group:function(){
		    
		        for(var i=0;i<this.tree.length;i++){
		            if(this.groups[this.tree[i].superId]){
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }else{
		                this.groups[this.tree[i].superId]=[];
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }
		        }

		    },

		    getDom:function(a){
		    	
		    	
		        if(!a){return ''}
		       flag=0;
		        var html="";
		    
		    	if(a[0].superId==0)
		    	{
		    			for(var i=0;i<a.length;i++)
		    			{
		    				if(className=="#treeTable2")
		    				{
								html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td> <td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    				
		    				}
		    				else
		    				{
		    					html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td> <td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    				}
		    				$(className).append(html);
		    				
		    			}
		    			
		    			flag=1;
		    	}
		    	if(flag==0)
		    	{
		    			var length=a.length;
		    			var superid=0;
		    			for(var i=0;i<length;i++)
		    			{	
		    				superid=a[i].superId;
		    				if(isChilde(a[i].id,object))
		    				{ 
		    					if(className=="#treeTable2")
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td><td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    				
		    					}
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td><td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    				
		    					}
		    				}  
		    				else
		    				{  		
		    					if(className=="#treeTable2")
		    					{
		    						html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td><td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    					}
		    					else
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td>'+a[i].remark+'</td><td>'+a[i].level+'</td><td class="stopEvent"><a href="javascript:void(0)" data-id='+a[i].id+'  class="btn btn-primary btn-stop btn-xs up" >上移</a><a href="javascript:void(0)" data-id='+a[i].id+' class="btn btn-info btn-stop btn-xs dowm" >下移</a></td> </tr>\n';		
		    					
		    					}
		    				}
		    				
		    				
		    				    
		    			}
		    				if(className=="#treeTable1")
		    				{
		    					childer.push({superid:superid,html:html});
		    				}
		    				else
		    				{
		    					childer2.push({superid:superid,html:html});
		    				}

		    			
		    		
		    		
		    			
		    	}
		    	 
		        for(var i=0;i<a.length;i++)
		        {
		           this.getDom(this.groups[a[i].id]);

		        }

	        /*html+='</optgroup>\n';*/

	       /* $("#"+id).append(html);*/
	        return html;
	    }
	   

	}
 var html=new treeMenu(object).init(0);
option(className);
}
	




//判断是否有子节点
function isChilde(id,object)
{
	for(var i=0;i<object.length;i++)
	{
		if(object[i].superId==id)
		{
			return true;
		}
	}
	return false;
}
	function option(className)
	{			
			var option = {
	                theme:'vsStyle',
	              
	                beforeExpand : function($treeTable, id) {

	            	var html="";
	                    //判断id是否已经有了孩子节点，如果有了就不再加载，这样就可以起到缓存的作用
	                    if ($('.' + id, $treeTable).length) { return; }
	            
	                   var index=jq(className).find("tr").eq(id).attr("id");
	                   if(className=="#treeTable2")
		    			{
		                   for(var i=0;i<childer2.length;i++)
		                   {
		                   	if(id==childer2[i].superid)
		                   	{
		                   		html=childer2[i].html;
		                   	}
		                   }
		                 }
		                 else
		                 {
		                 	 for(var i=0;i<childer.length;i++)
		                   	{
		                   		if(id==childer[i].superid)
		                   		{
		                   			html=childer[i].html;
		                   		}
		                  	 }
		                 }
	                
	                     $treeTable.addChilds(html);
					},
					 onSelect : function($treeTable, id) {
	                    window.console && console.log('onSelect:' + id);
	                    
	                }


		}	
	
		$(className).treeTable(option);
	}




//上移和下移
function upAndDown()
{
	//上移
	 var $up = $(".up") 
	$(document).on("click",".up",function(){
		 var $tr = $(this).parents("tr"); 
		 var level=$tr.find("td").eq(3).html();
		 var level1=$tr.prev().find("td").eq(3).html();
		 var superid=$tr.attr("data-superid");
		 var superid2=$tr.prev().attr("data-superid");
		 $tr.attr("isChange",true);
		 $tr.prev().attr("isChange",true);
		 if(superid==superid2)//判断是否是同一级
		 {
		 	if ($tr.index() != 1) 
		 	{ 
	            $tr.fadeOut().fadeIn(); 
	            $tr.prev().before($tr); 
	            $tr.find("td").eq(3).html(level1);//不改变td优先级
	            $tr.next().find("td").eq(3).html(level);
	            $tr.attr("isChange",true);
		 		$tr.prev().attr("isChange",true);
       		 } 
		 }
		$(".title").attr("isChange",false);
	})
   
    //下移
    var $dowm = $(".dowm"); 
    var len = $dowm.length; 
  	$(document).on("click",".dowm",function(){ 
        var $tr = $(this).parents("tr"); 
        var level=$tr.find("td").eq(3).html();
		var level1=$tr.next().find("td").eq(3).html();
		var superid=$tr.attr("data-superid");
		var superid2=$tr.next().attr("data-superid");
		
		if(superid==superid2)//判断是否是同一级
		{
	        if ($tr.index() != len - 1) { 
	            $tr.fadeOut().fadeIn(); 
	            $tr.next().after($tr);
	            $tr.find("td").eq(3).html(level1);
	            $tr.prev().find("td").eq(3).html(level); 
	            $tr.attr("isChange",true);//是否移动
				$tr.next().attr("isChange",true);
	        } 
	    }
	    $(".title").attr("isChange",false);
    }); 
}
}]);


